<?php

namespace common\observer;

use common\model\TaskModel;
use common\service\StepService;
use support\SingletonTrait;
use common\service\CommonService;
use common\service\StatusService;
use teamones\dbeventObserver\interfaces\SplSubject;
use teamones\dbeventObserver\interfaces\SplObserver;
use think\Hook;

/**
 * 任务计划时间变化 触发hook通知
 */
class TaskPlanTimeUpdate implements SplObserver
{
    use SingletonTrait;

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        $updateKeys = [];
        foreach (['plan_delivery_time'] as $checkColumn) {
            if (isset($updateData[$checkColumn])) {
                $updateKeys[] = $checkColumn;
            }
        }
        if (empty($updateKeys)) {
            return;
        }
        $task = $subject->getSubjectData();
        $hookData = [
            'type' => 'update_plan',
            'task_id' => $task['id'],
            'update_keys' => $updateKeys,
        ];
        try {
            Hook::listen('task_update', $hookData);
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
